//
//  CYBillViewController.m
//  cyjf
//
//  Created by tauCross on 2017/8/30.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYBillViewController.h"

#import "CYBillCell.h"
#import "CYEmptyCell.h"



@interface CYBillViewController ()

@property(nonatomic, assign)NSInteger pageNo;
@property(nonatomic, assign)BOOL loading;
@property(nonatomic, assign)BOOL hasMore;

@property(nonatomic, assign)BOOL errorShowed;

@property(nonatomic, strong)UIView *noneBillView;
@property(nonatomic, strong)UIImageView *noneBillImageView;
@property(nonatomic, strong)UILabel *noneBillLabel;

@property(nonatomic, strong)NSString *hint;

@end


@implementation CYBillViewController

- (void)loadUI
{
    self.navigationItem.title = @"资金记录";
    self.tableView.hidden = YES;
    
    self.noneBillView = [[UIView alloc] init];
    self.noneBillView.hidden = YES;
    [self.contentView addSubview:self.noneBillView];
    {
        self.noneBillImageView = [[UIImageView alloc] initWithImage:CYImage(@"bill_none")];
        [self.noneBillView addSubview:self.noneBillImageView];
        
        self.noneBillLabel = [[UILabel alloc] init];
        self.noneBillLabel.text = @"账单列表为空，赶紧去投资吧！";
        self.noneBillLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
        self.noneBillLabel.textColor = HEXCOLOR(@"999999");
        [self.noneBillLabel sizeToFit];
        [self.noneBillView addSubview:self.noneBillLabel];
    }
}

- (void)layoutUI
{
    self.noneBillView.frame = self.contentView.bounds;
    {
        self.noneBillImageView.centerX = self.noneBillView.width / 2;
        self.noneBillImageView.top = 50;
        
        self.noneBillLabel.centerX = self.noneBillView.width / 2;
        self.noneBillLabel.centerY = self.noneBillImageView.bottom + 23;
    }
}

- (void)loadData
{
    self.pageNo = 1;
    self.loading = NO;
    self.hasMore = YES;
    
    self.tableModel = [[NIMutableTableViewModel alloc] initWithSectionedArray:@[] delegate:(id)[NICellFactory class]];
    self.tableView.dataSource = self.tableModel;
    [self.tableView reloadData];
    
    [self loadDataFromServer];
    [self loadHintFromServer];
}

- (void)loadHintFromServer
{
    @weakify(self);
    [CYStandardDataAPI user_userBillInfoWithComplete:^(CYResponse *response) {
        @strongify(self);
        self.hint = nil;
        if(response.code == 0)
        {
            NSString *hint = SAFE_STRING(response.content);
            if([hint isKindOfClass:[NSString class]] && hint.length)
            {
                self.hint = hint;
            }
        }
        [self.tableView reloadData];
    }];
}

- (void)loadDataFromServer
{
    if(self.loading)
    {
        return;
    }
    if(self.hasMore == NO)
    {
        return;
    }
    self.loading = YES;
    @weakify(self);
    [CYStandardDataAPI user_userBillDetailWithPageNo:@(self.pageNo) complete:^(CYResponse *response) {
        @strongify(self);
        if(response.code == 0)
        {
            self.hasMore = NO;
            NSMutableArray *cells = [NSMutableArray array];
            if([response.content isKindOfClass:[NSArray class]])
            {
                if(self.pageNo == 1)
                {
                    if([response.content count] > 0)
                    {
                        self.tableView.hidden = NO;
                    }
                    else
                    {
                        self.noneBillView.hidden = NO;
                    }
                }
                self.hasMore = [response.content count] >= CY_API_PAGE_SIZE;
                self.pageNo ++;
                for(NSDictionary *bill in response.content)
                {
                    if([bill isKindOfClass:[NSDictionary class]])
                    {
                        CYBillCellObject *cell = [CYBillCellObject objectWithCellClass:[CYBillCell class]];
                        cell.model = [CYDataAPIBillModel mj_objectWithKeyValues:bill];
                        [cells addObject:cell];
                    }
                }
            }
            [self.tableModel addObjectsFromArray:cells];
            [self.tableView reloadData];
            self.loading = NO;
        }
        else
        {
            if(self.errorShowed == NO)
            {
                [SVProgressHUD showErrorWithStatus:response.message];
                self.errorShowed = YES;
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                @strongify(self);
                self.loading = NO;
            });
        }
    }];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView == self.tableView)
    {
        if(scrollView.contentSize.height - scrollView.contentOffset.y < scrollView.height * 2)
        {
            [self loadDataFromServer];
        }
    }
}

#pragma mark - UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    if(self.hint)
    {
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        label.numberOfLines = 0;
        label.text = self.hint;
        label.textColor = HEXCOLOR(@"EFA839");
        label.size = [label sizeThatFits:CGSizeMake(SCREEN_WIDTH - 64, MAXFRAG)];
        view.width = SCREEN_WIDTH;
        view.height = MAX(50, label.height + 28);
        view.backgroundColor = HEXCOLOR(@"FFFBEA");
        label.centerY = view.height / 2;
        label.left = 41;
        [view addSubview:label];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:CYImage(@"ico_money_record_rmb_yellow")];
        imageView.left = 20;
        imageView.top = label.top;
        [view addSubview:imageView];
    }
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(self.hint)
    {
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        label.numberOfLines = 0;
        label.text = self.hint;
        return MAX(50, [label sizeThatFits:CGSizeMake(SCREEN_WIDTH - 64, MAXFRAG)].height + 28);
    }
    else
    {
        return 0;
    }
}

@end
